package com.ruoyi.manage.organization.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ObjUtil;
import com.ruoyi.common.utils.StrUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.manage.organization.domain.BizOrganizationAudit;
import com.ruoyi.manage.organization.domain.vo.BizOrganizationAuditVo;
import com.ruoyi.manage.organization.service.IBizOrganizationAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 金融机构审核Controller
 *
 * @author ruoyi
 * @date 2025-08-15
 */
@Api(tags="金融机构审核控制器")
@RestController
@RequestMapping("/manage/audit" )
public class BizOrganizationAuditController extends BaseController {
    /**
    * 配置表别名 默认空
    */
    private final static String TABLE_PREFIX=StrUtil.initTableAlias(null);
    @Autowired
    private IBizOrganizationAuditService bizOrganizationAuditService;

    /**
     * 查询金融机构审核列表
     */
    @ApiOperation(value = "查询金融机构审核",notes = "金融机构审核管理")
    @PreAuthorize("@ss.hasPermi('manage:audit:list')" )
    @GetMapping("/list" )
    public TableDataInfo list(BizOrganizationAuditVo bizOrganizationAudit, QueryWrapper<BizOrganizationAudit> queryWrapper) {
        setQueryCondition(bizOrganizationAudit,queryWrapper);
        startPage();
        List<BizOrganizationAudit> list = bizOrganizationAuditService.list(queryWrapper);
        return getDataTable(list);
    }
    private void setQueryCondition(BizOrganizationAuditVo bizOrganizationAudit, QueryWrapper<BizOrganizationAudit> queryWrapper) {
        if(ObjUtil.isNotNull(bizOrganizationAudit.getOrgId())){
            queryWrapper.eq(TABLE_PREFIX+"org_id" ,bizOrganizationAudit.getOrgId());
        }
        if(StrUtil.isNotBlank(bizOrganizationAudit.getAudit())){
            queryWrapper.eq(TABLE_PREFIX+"audit" ,bizOrganizationAudit.getAudit());
        }
        if(StrUtil.isNotBlank(bizOrganizationAudit.getAuditDesc())){
            queryWrapper.eq(TABLE_PREFIX+"audit_desc" ,bizOrganizationAudit.getAuditDesc());
        }
        if(bizOrganizationAudit.getAuditTime() != null){
            queryWrapper.eq(TABLE_PREFIX+"audit_time" ,bizOrganizationAudit.getAuditTime());
        }
        if(bizOrganizationAudit.getDeleted() != null){
            queryWrapper.eq(TABLE_PREFIX+"deleted" ,bizOrganizationAudit.getDeleted());
        }
        if(StrUtil.isNotBlank(bizOrganizationAudit.getVersion())){
            queryWrapper.eq(TABLE_PREFIX+"version" ,bizOrganizationAudit.getVersion());
        }
        queryWrapper.orderByDesc(TABLE_PREFIX+"create_time");
    }

    /**
     * 导出金融机构审核列表
     */
    @PreAuthorize("@ss.hasPermi('manage:audit:export')" )
    @PostMapping("/export" )
    public void export(HttpServletResponse response, BizOrganizationAuditVo bizOrganizationAudit,QueryWrapper<BizOrganizationAudit> queryWrapper) {
        if (CollUtil.isEmpty( bizOrganizationAudit.getIds())){
            setQueryCondition(bizOrganizationAudit,queryWrapper);
        }else {
            queryWrapper.in(TABLE_PREFIX+"id",bizOrganizationAudit.getIds());
        }
        List<BizOrganizationAudit> list = bizOrganizationAuditService.list(queryWrapper);
        ExcelUtil<BizOrganizationAudit> util = new ExcelUtil<BizOrganizationAudit>(BizOrganizationAudit. class);
        util.exportExcel(response,list, "audit");
    }

    /**
     * 获取金融机构审核详细信息
     */
    @ApiOperation(value = "获取金融机构审核",notes = "金融机构审核管理")
    @PreAuthorize("@ss.hasPermi('manage:audit:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(bizOrganizationAuditService.queryGetById(id));
    }

    /**
     * 新增金融机构审核
     */
    @ApiOperation(value = "新增金融机构审核",notes = "金融机构审核管理")
    @PreAuthorize("@ss.hasPermi('manage:audit:add')" )
    @RepeatSubmit
    @PostMapping
    public AjaxResult add(@Validated @RequestBody BizOrganizationAudit bizOrganizationAudit) {
        return toAjax(bizOrganizationAuditService.addBizOrganizationAudit(bizOrganizationAudit) ? 1 : 0);
    }

    /**
     * 修改金融机构审核
     */
    @ApiOperation(value = "修改金融机构审核",notes = "金融机构审核管理")
    @PreAuthorize("@ss.hasPermi('manage:audit:edit')" )
    @PutMapping
    public AjaxResult edit(@RequestBody BizOrganizationAudit bizOrganizationAudit) {
        return toAjax(bizOrganizationAuditService.updateBizOrganizationAudit(bizOrganizationAudit) ? 1 : 0);
    }

    /**
     * 删除金融机构审核
     */
    @ApiOperation(value = "删除金融机构审核",notes = "金融机构审核管理")
    @PreAuthorize("@ss.hasPermi('manage:audit:remove')" )
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(bizOrganizationAuditService.deleteBizOrganizationAuditByIds(ids) ? 1 : 0);
    }
}
